<?php

namespace EM\NewsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class AdminController extends Controller
{
    
    public function indexAction()
    {
        
        $user = $this->container->get('security.context')->getToken()->getUser();
        $categories=$this->getDoctrine()->getRepository('EMNewsBundle:Categories');
        $catnew_table=$this->getDoctrine()->getRepository('EMNewsBundle:NewsCategories');
        
        if($this->get('security.context')->isGranted('ROLE_ADMIN')){
        ECHO "ADMIN";
            $news=$this->getDoctrine()->getRepository('EMNewsBundle:News')->findAll();
            $permissions=$this->getDoctrine()->getRepository('EMNewsBundle:UserGeoLangSettings')->findByUser($user->getId());
           // print_r($permissions);
            //exit();
            foreach($permissions as $permission){
                echo $permission->getUser()->getId();
                echo $permission->getGeoLang()->getId();
                
            }            
		$total = count($news);
                $dt=new \DateTime();
                $dt->sub(new \DateInterval('P10D'));
                return $this->render('EMNewsBundle:Admin:index.html.twig',
                array('total' => $news,
                    'dt' => $dt,
                    ));
         
        }
        if($this->get('security.context')->isGranted('ROLE_EDITOR')){
            ECHO "ROLE_EDITOR";
            $news=$this->getDoctrine()->getRepository('EMNewsBundle:News')->findByStatus('P');
            $permissions=$this->getDoctrine()->getRepository('EMNewsBundle:UserGeoLangSettings')->findByUser($user->getId());
            $total = count($news);
		$tcat = $categories->findAll();
                $catn=$catnew_table->findAll();
		//$utils = $this->get('em_service');
			$dt=new \DateTime();
			$dt->sub(new \DateInterval('P10D'));
			return $this->render('EMNewsBundle:Author:index.html.twig',
                        array('total' => $news,
                        'dt' => $dt,
                        'categories' => $tcat,
                        'catn' => $catn,
                        ));
         
        }
        if($this->get('security.context')->isGranted('ROLE_AUTHOR')){
            ECHO "AUTHOR";
		$news=$this->getDoctrine()->getRepository('EMNewsBundle:News')->findByAuthor($user->getId());
		
		$total = count($news);
		$tcat = $categories->findAll();
                $catn=$catnew_table->findAll();
		//$utils = $this->get('em_service');
			$dt=new \DateTime();
			$dt->sub(new \DateInterval('P10D'));
			return $this->render('EMNewsBundle:Author:index.html.twig',
                        array('total' => $news,
                        'dt' => $dt,
                        'categories' => $tcat,
                        'catn' => $catn,
                        ));
            
        }
       
    }

   public function saveSettingsAction(){
       return $this->render('EMNewsBundle:Settings:index.html.twig');
   }
   public function deniedAction(){
       return $this->render(
            'EMNewsBundle:Error:403.html.twig'
        );
   }
	
}
